package com.tianqu.tms.www.transfer.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;
/**
 * 打印变更表 查询
 * @author User
 *
 */
public class PChangeTableSearchResultAction extends TmsAction {
	private Logger logger = Logger
			.getLogger(PChangeTableSearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TransferRgActionForm inForm = (TransferRgActionForm) form;
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		Codes code = Codes.getInstance();
		String serial = inForm.getSerial();

		TransferAcceptedInfo accept = new TransferAcceptedInfo();
//		accept = accept.getAllAcceptInfoBySerial(serial,"");
		List<TransferAcceptedInfo> accepts = accept.getAcceptInfoList(serial, "");
		if (accepts != null) {
		    if (accepts.size() == 0) {
		        //  # 如果没有符合条件数据，那么页面提示“【U0039】查询的结果不存在。”
                setErrorsToRequest(request, TmsConstants.U0039, null);
                return mapping.getInputForward();
		    } else if (accepts.size() > 1) {
		        //  # 如果有多条数据，那么页面提示“【U1405】出现重复流水号业务，请联系系统管理员。”
                setErrorsToRequest(request, TmsConstants.U1405, null);
                return mapping.getInputForward();
		    } else if (accepts.size() == 1) {
		        accept = accepts.get(0);
		    }

	        TmsUserProfile profile = uc.getUserProfile();
	        if (profile.getUserType() == 2 && accept.isSameMemberIdBetweenAttnAndCar(profile.getLoginId(),serial,"") == false) {
	            // a）如果操作人类型是商户会员，即“当前操作人对应tms_user表的userType为：2，商户会员”根据车辆流水号（serial），
	            // 搜索“过户业务基本信息表（tms_transfer_accepted_info）”，
	            // 如果过户业务基本信息表（tms_transfer_accepted_info）中的insertuserid在tms_attn中对应数据的memberid与当前操作人所属商户id（memberid）不符，
	            // 则提示“此车辆非您所在车行办理车辆，您无权处理此车。”
	            setErrorsToRequest(request, TmsConstants.U12001 ,null);
                return mapping.getInputForward();
	        }
	        if (profile.getUserType() == 1 && accept.getChangeEngine() == 2) {
	            // b）如果操作人类型是操作人员，即“当前操作人对应tms_user表的userType为：1，操作人员”根据车辆流水号（serial），
	            // 搜索“过户业务基本信息表（tms_transfer_accepted_info）”中“变更表打印缴费状态（changeEngine）”。
	            // 如果“变更表打印缴费状态（changeEngine）”为“2，未缴费”，则页提示“当前车辆未缴纳变更费用，请缴费后进行查询。”
	            setErrorsToRequest(request, TmsConstants.U12002 ,null);
                return mapping.getInputForward();
	        }

		    
		    
    		if (accept != null) {


    	        if (profile.getUserType() == 1) {
    	            // c）如果操作人类型是操作人员，即“当前操作人对应tms_user表的userType为：1，操作人员”根据车辆流水号（serial），
    	            // 分别查询“过户业务基本信息表（tms_transfer_accepted_info）”中"变更表原车主车主身份证/信用代码变更（changesVacate）“与”机动车所有人姓名/名称变更（changeColor）“，
    	            // 如果值为 2，否，则页面初始化的时候，此项不允许更改。
    	            if (accept.getChangesVacate() == 2) {
    	                view.setReadOnlyChangesVacate("Y");
    	            }
    	            if (accept.getChangeColor() == 2) {
    	                view.setReadOnlyChangeColor("Y");
    	            }
    	        }
    			view.setSerial(serial);
    			view.setStatus(code.getItemValueByItemIdAndItemCode(
    					Codes.ACCEPT_STATUS, accept.getStatus()));
    			view.setLicensePlate(accept.getLicensePlate());
    			view.setVin(accept.getVin());
    			view.setAgentName(accept.getAgentName());
    			view.setAgentOwnerIdCardNo(accept.getAgentOwnerIdCardNo());
    			view.setAgentAddress(accept.getAgentAddress());
    			view.setAgentPhone(accept.getAgentPhone());
    			view.setNewCarOwner(accept.getNewcarOwner());
    			view.setNewOwnerAddress(accept.getNewOwnerAddress());
    			view.setNewOwnerPhone(accept.getNewOwnerPhone());
    			view.setOwnerType(Codes.getInstance().getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, accept.getOwnerType()));
    			if (accept.getCarType() != null) {
    			    view.setCardType(Codes.getInstance().getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, Integer.valueOf(accept.getCarType())));
    			}
    			view.setCarOwner(accept.getCarOwner());
    			view.setOwnerPhone(accept.getOwnerPhone());
    			view.setOwnerAddress(accept.getOwnerAddress());
    			
    			// # 原车主身份证/代码证，文本显示，对应值为，如果原车主类别（ownerType）为“1：个人”，那么显示原车主身份证号（carOwnerIdCardNo），否则显示原车主组织机构代码证号（ownerOrgNo）
    			if (1 == accept.getOwnerType()) {
                    view.setCarOwnerIdCardNo(accept.getCarOwnerIdCardNo());
    			} else {
                    view.setCarOwnerIdCardNo(accept.getOwnerOrgNo());
    			}
    	        view.setModelCode(accept.getModelCode());
    	        view.setVendorCode(accept.getVendorCode());
    	        if (accept.getCarUse() != null && "".equals(accept.getCarUse())) {
    	            view.setCarUse(Codes.getInstance().getItemValueByItemIdAndItemCode(Codes.USER_NATURE,Integer.parseInt(accept.getCarUse())));
    	        }
//    	        view.setSendCardDate(accept.getSendCardDate());
    	        view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,
    	                accept.getOwnerType()));
    	        view.setCarOwner(accept.getCarOwner());
    	        view.setCarOwnerIdCardNo(accept.getCarOwnerIdCardNo());
    	        view.setOwnerPhone(accept.getOwnerPhone());
    	        view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,
    	                accept.getCardType()));
    	        view.setOwnerAddress(accept.getOwnerAddress());
    
//    			if (accept.getMarketType() == 1) {
//    				Car car = Car.getInstance();
//    				car = car.findCarBySerial(accept.getBasicCarId());
//    				if (car != null) {
//    					view = toViewHelper(car, view);
//    				}
//    			}
//    			if (accept.getMarketType() == 2) {
//    				TransferPersonalCarInfo person = new TransferPersonalCarInfo();
//    				person = person.getPersonalCarInfoBySerial(accept
//    						.getBasicCarId());
//    				if (person != null) {
//    					view = toViewHelper1(person, view);
//    				}
//    			}
    			view.setUserType(String.valueOf(profile.getUserType()));
//    			TransferProcessHistory his = new TransferProcessHistory();
//    			view.setLcList(his.loadtHistoryBySerial(inForm.getSerial()));// 历史记录
    		} else {
    			setErrorsToRequest(request, TmsConstants.IM006, null);
    			return mapping.getInputForward();
    		}
        } else {
            //  # 如果没有符合条件数据，那么页面提示“【U0039】查询的结果不存在。”
            setErrorsToRequest(request, TmsConstants.U0039, null);
            return mapping.getInputForward();
        }
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was compeleted.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

//	/**
//	 * entity -> viewhelper
//	 * 
//	 * @param entity
//	 * @return
//	 * @throws TmsDaoException
//	 */
//	private TransferRgInputViewHelper toViewHelper(Car entity,
//			TransferRgInputViewHelper view) throws TmsDaoException {
//
//		Codes code = Codes.getInstance();
//		view.setLicensePlate(entity.getLicensePlate());
//		view.setVin(entity.getVin());
//		view.setModelCode(entity.getModelCode());
//		view.setVendorCode(entity.getVendorCode());
//		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,
//				entity.getCarUse()));
//		view.setSendCardDate(entity.getSendCardDate());
//		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,
//				entity.getOwnerType()));
//		view.setCarOwner(entity.getCarOwner());
//		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
//		view.setOwnerPhone(entity.getOwnerPhone());
//		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,
//				entity.getCardType()));
//		view.setOwnerAddress(entity.getOwnerAddress());
//
//		return view;
//	}
//
//	/**
//	 * entity -> viewhelper
//	 * 
//	 * @param entity
//	 * @return
//	 * @throws TmsDaoException
//	 */
//	private TransferRgInputViewHelper toViewHelper1(
//			TransferPersonalCarInfo entity, TransferRgInputViewHelper view)
//			throws TmsDaoException {
//
//		Codes code = Codes.getInstance();
//		view.setLicensePlate(entity.getLicensePlate());
//		view.setVin(entity.getVin());
//		view.setModelCode(entity.getModelCode());
//		view.setVendorCode(entity.getVendorCode());
//		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,
//				entity.getCarUse()));
//		view.setSendCardDate(entity.getSendCardDate());
//		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,
//				entity.getOwnerType()));
//		view.setCarOwner(entity.getCarOwner());
//		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
//		view.setOwnerPhone(entity.getOwnerPhone());
//		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,
//				entity.getCardType()));
//		view.setOwnerAddress(entity.getOwnerAddress());
//		return view;
//	}
}